--[[
Author: wangxiaoyan
Date: 2020-09-27 15:07:06
LastEditTime: 2020-12-02 20:58:22
FilePath: \Lua\View\UIBase.lua

UI基类
1. 为UI提供更加明确的流程控制
    OnCreate    gameObject创建成功后触发
    OnEnable    显示时触发
    OnDisable   隐藏时触发
    OnDestroy   销毁时触发
2. 对象清理
    注册的事件和消息会在UI卸载时自动清理
3. 提供更加方便的方法
4. 提供UIComponent功能 方便编写UI组件
--]]

require 'Common/define'
require 'Logic/Config'
require 'Common/functions'
require 'Common/Class'
require 'View/UIBase/UIComponentInterface'
---@class UIBase : UIComponentInterface
---@field New fun():UIBase
UIBase = Class("UIBase", UIComponentInterface)

local _M = UIBase

-- 初始化（gameobject创建之前）
function _M:Init(func)
    self.backFunc = func
    local name, packageName = getUIName(self._panelName)
    panelMgr:ShowUI(name, packageName, self.Create,Config.useLocalUI,self)
end

function _M:ctor(panelName)
    self._panelName = panelName
    rawset(self,"_eventList",{})
    rawset(self,"_components",{})
end

-- 创建（gameobject创建之后）
function _M:Create(obj)
    -- TODO WG 这样处理的原因是显示阶段也会触发回调，理论上只有加载完成后才能回调
    -- 目前是为了兼容以前的写法
    if self.fguiObj ~= nil then
        return
    end
    ---@type GComponent
    self.fguiObj = obj
    --[[self._luaBehaviour = self.fguiObj.displayObject.gameObject:GetComponent('LuaBehaviour')
    if self._luaBehaviour then
        self._luaBehaviour.luaFuncTable = self
        self._luaBehaviour.luaOnEnableFunc = self.Enable
        self._luaBehaviour.luaOnDisableFunc = self.Disable
        self._luaBehaviour.luaOnDestroyFunc = self.Destroy
    end]]

    self:OnComCreate()
    self:OnCreate()

    self.fguiObj.visible = false
    self.fguiObj.visible = true
end
function _M:OnCreate()end

-- 显示
function _M:Enable()
    if self == nil then
        return
    end
    self:OnEnable()
    self:OnComEnable()
end
function _M:OnEnable()

end

-- 隐藏
function _M:Disable()
    self:OnComDisable()
    self:OnDisable()
end
function _M:OnDisable()
end

-- 卸载
function _M:Destroy()
    self:UnRegisterEvent()

    self.fguiObj = nil
    if self._luaBehaviour then
        self._luaBehaviour.luaFuncTable = nil
        self._luaBehaviour.luaOnEnableFunc = nil
        self._luaBehaviour.luaOnDisableFunc = nil
        self._luaBehaviour.luaOnDestroyFunc = nil
        self._luaBehaviour = nil
    end

    self:OnComDestroy()
    self:OnDestroy()
end
function _M:OnDestroy()
end


-- 注册Event
function _M:RegisterEvent(key,onCall,handle)
    if self._eventList == nil then
        return
    end
    if self._eventList[key] then
        logError("RegisterEvent 重复的key "..key)
        CloseEvent(key,self._eventList[key])
    end
    local handle = handle or self
    local betterOnCall = function(...)
        if isOpenUI(self._panelName) then
            if handle == nil then
                onCall(...)
            else
                onCall(handle,...)
            end
        end
    end
    ListenEvent(key,betterOnCall)
    self._eventList[key] = betterOnCall
end
function _M:RemoveEvent(key)

    local c = self._eventList[key]
    if not c then return end
    CloseEvent(key,c)
end
-- 卸载Event
function _M:UnRegisterEvent()
    if self._eventList == nil then
        return
    end

    for key, value in pairs(self._eventList) do
        CloseEvent(key,value)
    end

    table.clear(self._eventList)
end

-- 主动清除回调
function _M:clearBackFunc()
    self.backFunc = nil
end

-- 返回上一界面
function _M:OnBack()
    closeUI(self._panelName)
    if self.backFunc ~= nil then
        if type(self.backFunc) == "function" then
            self.backFunc()
        else
            logError("参数传递错误, 请立马告知程序,UIName: "..self._panelName);
        end
    end
    self.backFunc = nil
end

--增加一个组件(luaCom继承UIComponentBase)
---@param obj GComponent
---@param luaCom UIComponentBase
function _M:CreateComponent(luaCom,obj,...)
    if self._components == nil then
        return
    end
    local tCom = luaCom.New(obj,...)
    tCom._parent = self
    table.insert(self._components,tCom)
    return tCom
end
--删除一个组件
function _M:DestroyComponent(com)
    if self._components == nil then
        return
    end
    table.removeByValue(self._components,com)
end
function _M:OnComCreate()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:OnComCreate()
    end
end
function _M:OnComDestroy()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:OnComDestroy()
    end
    table.clear(self._components)
end
function _M:OnComEnable()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:OnComEnable()
    end
end
function _M:OnComDisable()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:OnComDisable()
    end
end